package com.xudow.app.newui.task;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.activeshare.edu.ucenter.common.messages.course.ListCourseMessage;
import com.google.gson.Gson;
import com.xudow.app.Config;
import com.xudow.app.XApplication;
import com.xudow.app.directseeding.entity.LiveRoom;
import com.xudow.app.util.rest.XRestTemplate;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 获取课程列表Task
 *
 * @author lukecheng
 */
public class LiveListTask extends AsyncTask<HashMap<String, String>, Void, Void> {
    private static final String TAG = makeLogTag(LiveListTask.class);

    private String url = Config.BASE_URL + Config.COURSE_LIST_URL;

    public static final int SUCCESS = 0;
    public static final int ERROR = 1;

    private Context context;
    private Handler handler;

    public LiveListTask(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    public LiveListTask(Context context, Handler handler, String url) {
        this.context = context;
        this.handler = handler;
        this.url = url;
    }

    private void sendErrorMessage(String msg) {
//        android.os.Message message = new android.os.Message();
//        message.what = ERROR;
//
//        if (null != msg) {
//            Bundle data = new Bundle();
//            data.putString("errMessage", msg);
//            message.setData(data);
//        }
//
//        handler.sendMessage(message);
        Toast.makeText(context,"网络错误",Toast.LENGTH_SHORT).show();
    }

    private void sendSuccessMessage(List<LiveRoom.ScreenEntity> courses) {
        android.os.Message message = new android.os.Message();
        message.what = SUCCESS;

        Bundle data = new Bundle();

        if (courses != null) {
//            ListCourseMessage.CourseListEntity[] courseArr = new ListCourseMessage.CourseListEntity[courses.size()];
//            for (int i = 0; i < courses.size(); i++) {
//                courseArr[i] = courses.get(i);
//            }

            data.putSerializable("courses", (Serializable) courses);
            Log.i("CourseListTask","courses>>"+courses.size());
            message.setData(data);
        }

        handler.sendMessage(message);
    }

    @Override
    protected Void doInBackground(HashMap<String, String>... parameters) {
        XApplication application = (XApplication) context.getApplicationContext();

        XRestTemplate restTemplate = new XRestTemplate(application.getCookieStore(), context);
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        // restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        LinkedMultiValueMap<String, String> valueMap = new LinkedMultiValueMap<String, String>();

        HashMap<String, String> params = parameters[0];

        Log.i("CourseListTask","url>>>>"+url);

        //分页条数
        if (!android.text.TextUtils.isEmpty(params.get("offset"))) {
            valueMap.add("offset", params.get("offset"));
            Log.i("CourseListTask", "offset>>>>" + params.get("offset"));
        }
        //第几页
        if (!android.text.TextUtils.isEmpty(params.get("page"))) {
            valueMap.add("page", params.get("page"));
            Log.i("CourseListTask", "page>>>>" + params.get("page"));
        }


            try {
                String result = restTemplate.postForObject(url, valueMap,
                        String.class);

                Log.i("CourseListTask", "result>>>>" +result);
                LiveRoom listCourseMessage = null;
                listCourseMessage = new Gson().fromJson(result.toString(), LiveRoom.class);
                Log.i("CourseListTask","listCourseMessage>>>"+listCourseMessage);
                if (listCourseMessage != null) {

                    //FileUtils.writeFile(result1,new File(context.getCacheDir().getAbsolutePath(),"courseList1.txt"));
                    if (listCourseMessage.getResult().equals("0")) {

                        sendSuccessMessage(listCourseMessage.getScreen());

                    } else if (listCourseMessage.getResult().equals("1")) {
                        Log.i("CourseListTask","listCourseMessage>>>"+listCourseMessage.getErrorMessage());
                        sendErrorMessage(listCourseMessage.getErrorMessage());

                    }
                } else {
                    Log.i("CourseListTask", "else>>>>sendErrorMessage(null);");
                    sendErrorMessage(null);

                }

            } catch (Exception e) {
                sendErrorMessage(e.getMessage());
                Log.i("CourseListTask", "Exception>>>>" + e.getMessage());
//                      Log.i("listCourseMessage","listCourseMessage>>"+listCourseMessage);
//                      Log.i("listCourseMessage","listCourseMessageeee>>"+e);

            }


        return null;
    }
}
